package com.threadgroup;

/**
 * @PackageName:com.threadgroup
 * @ClassName:test
 * @Description: 创建线程组
 * @Author: zhangxiaoyu
 * @date 2021/3/16 15:21
 */
public class Test {

    public static void main(String[] args){
         //1.返回当前main线程的线程组
        ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
        System.out.println(threadGroup);

        //2.定义线程组，如果不指定所属的线程组，则自动归属到当前线程所属的线程组中
        ThreadGroup group1 = new ThreadGroup("group1");
        System.out.println(group1);

        //3.定义线程组，同时指定父线程组
        ThreadGroup group2 = new ThreadGroup(threadGroup,"group2");

        //现在group1与group2 都是main线程中的子线程组 调用getParent返回父线程组

        System.out.println(group1.getParent() == threadGroup);
        System.out.println(group2.getParent() == threadGroup);

        //4.在创建线程时指定所属线程组
        Runnable r = new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread());
            }
        };

        //在创建线程时，如果没有指定线程组，则默认线程归属到父线程的线程组中
        //在main线程中创建了t1线程，称main线程为父线程，t1为子线程，t1没有指定线程组，则t1就归属到父线程main线程的线程组中
        Thread t1 = new Thread(r,"t1");
        System.out.println(t1);

        //创建线程时，可以指定线程所属的线程组
    }
}
